package butines.game2;

import butines.core.Mouse;

public class MouseHandler {

    // States //
    private int mouseX;
    private int mouseY;
    private boolean button1;
    private boolean button2;
    private boolean button3;
    private boolean shift;
    private boolean ctrl;
    private boolean alt;
    
    // update ever frame //
    private int lastx;
    private int lasty;
    
    private boolean[] button = new boolean[3];
    private boolean[] pressed = new boolean[3];

    private MouseEventListener listener;
    
    public MouseHandler() {

    }
    
    public MouseEventListener getListener() {
        return listener;
    }

    public void setListener(MouseEventListener listener) {
        this.listener = listener;
    }



    public int getMouseX() {
        return mouseX;
    }

    public int getMouseY() {
        return mouseY;
    }

    public boolean isButton1() {
        return button1;
    }

    public boolean isButton2() {
        return button2;
    }

    public boolean isButton3() {
        return button3;
    }

    public boolean isShift() {
        return shift;
    }

    public boolean isCtrl() {
        return ctrl;
    }

    public boolean isAlt() {
        return alt;
    }
    
    private void checkButton(Mouse mouse, int i) {
        if (button[i]) {
            if (!pressed[i]) {
                pressed[i] = true;
                fireMouseDown(mouse);
            }
        } else {
            if (pressed[i]) { 
                fireClick(mouse);
            }
            if (pressed[i]) {
                fireMouseUp(mouse);
                pressed[i] = false;
            }
        }
    }
    
    public void processEvent(Mouse mouse) {

        int mx = mouse.getMouseX();
        int my = mouse.getMouseY();
        
        button[0] = mouse.isButton1();
        button[1] = mouse.isButton2();
        button[2] = mouse.isButton3();
        
        if (lastx != mx || lasty != my) {
            fireMouseMove(mouse);
        }

        checkButton(mouse, 0);
        checkButton(mouse, 1);
        checkButton(mouse, 2);

        lastx = mx;
        lasty = my;

    }
    
    private MouseHandler update(Mouse mouse) {
        mouseX = mouse.getMouseX();
        mouseY = mouse.getMouseY();
        shift = mouse.isShift();
        ctrl = mouse.isCtrl();
        alt = mouse.isAlt();
        button1 = pressed[0];
        button2 = pressed[1];
        button3 = pressed[2];
        return this;
    }
    
    protected void fireClick(Mouse mouse) {
        if (listener != null) {
            listener.onClick(update(mouse));
        }
    }
    
    protected void fireMouseMove(Mouse mouse) {
        if (listener != null) {
            listener.onMouseMove(update(mouse));
        }
    }
    
    protected void fireMouseDown(Mouse mouse) {
        if (listener != null) {
            listener.onMouseDown(update(mouse));
        }
    }
    
    protected void fireMouseUp(Mouse mouse) {
        if (listener != null) {
            listener.onMouseUp(update(mouse));
        }
    }
    
}
